<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="main_ti box">
          <el-tabs v-model="activeName" @tab-click="handleClick" class="box">
            <el-tab-pane label="商品列表" name="1"></el-tab-pane>
            <el-tab-pane label="商品分类" name="2"></el-tab-pane>
          </el-tabs>
        </div>

        <!-- 搜索一 -->
        <div v-show="activeName == 1">
          <div class="mainor_form">
            <el-form
              :inline="true"
              :model="goods_form"
              class="demo-form-inline"
              ref="userListFrom1"
              label-width="100px"
              id="form"
            >
              <el-form-item label="" prop="date_type" class="wid100 methods">
                <el-select v-model="goods_form.date_type" placeholder="请选择">
                  <el-option label="配送日期" value="1"> </el-option>
                  <el-option label="下单日期" value="2"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="" prop="establishTime">
                <el-date-picker
                  v-model="establishTime"
                  type="daterange"
                  class="time220 marginLeft5s"
                  :editable="false"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                  :clearable="false"
                  :picker-options="pickerOptions"
                  @change="dateChange"
                />
              </el-form-item>
              <el-form-item label="商品名称：" prop="goods_spu_id">
                <change-goods-input
                  ref="changeGoodsInput"
                  @clear="
                    () => {
                      this.goods_form.goods_spu_id = '';
                    }
                  "
                  @sendValue="
                    (data) => {
                      this.goods_form.goods_spu_id = data.id;
                    }
                  "
                ></change-goods-input>
              </el-form-item>
              <el-form-item label="商品分类：" prop="category_id" class="wid220">
                <el-cascader
                  v-model="goods_form.category_id"
                  :options="options"
                  :props="defaultProps"
                  @change="getArticleList"
                ></el-cascader>
              </el-form-item>
              <MainFormBtn @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="moredata">
              </MainFormBtn>
              <div class="more el-form-item" @click="moredata = !moredata">
                <span>更多</span>
                <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
              </div>
              <div class="clearfix"></div>
              <el-collapse-transition>
                <div v-show="moredata" style="width: 100%">
                  <div>
                    <el-form-item label="支付方式：" prop="is_piroment">
                      <el-select
                        v-model.trim="goods_form.is_piroment"
                        placeholder="支付方式"
                        class="wid220"
                        @change="getArticleList"
                      >
                        <el-option label="所有" value=""></el-option>
                        <el-option
                          v-for="(is_piroment, index) in is_piroment"
                          :key="index"
                          :label="is_piroment"
                          :value="parseInt(index)"
                        ></el-option>
                      </el-select>
                    </el-form-item>

                    <el-form-item label="用户：" prop="uid">
                      <change-user-input
                        ref="changeUserInput"
                        @sendValue="getUser"
                        @clear="clearUid"
                        @change="getArticleList"
                      ></change-user-input>
                    </el-form-item>
                    <el-form-item
                      label="子账户："
                      prop="sub_zid"
                      class="marginBottom0"
                    >
                      <el-select
                        v-model.trim="goods_form.zid"
                        placeholder="子账户"
                        class="wid220"
                        filterable
                        @change="getArticleList"
                      >
                        <el-option
                          v-for="(subAccount, index) in address.subAccount"
                          :key="index"
                          :label="subAccount.nickname"
                          :value="subAccount.id"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                    <div class="clearfix"></div>
                    <el-form-item
                      label="用户分组："
                      prop="group_name"
                      class="marginBottom5"
                    >
                      <el-autocomplete
                        v-model.trim="group_name"
                        ref="company"
                        :trigger-on-focus="true"
                        @clear="
                          () => {
                            this.goods_form.group_id = '';
                          }
                        "
                        :fetch-suggestions="
                          (queryString, cb) => {
                            querySearchAsync(queryString, cb);
                          }
                        "
                        placeholder="用户分组"
                        clearable
                        class="wid220 main_comm"
                        :highlight-first-item="true"
                        value-key="nickname"
                        @select="
                          (item) => {
                            basicChange(item);
                          }
                        "
                      >
                        <template slot-scope="{ item }">
                          <div class="main_name">
                            <span>{{ item.title }}</span>
                          </div>
                        </template>
                      </el-autocomplete>
                    </el-form-item>

                    <el-form-item label="配送线路：" prop="line_id">
                      <el-select
                        v-model.trim="goods_form.line_id"
                        placeholder="配送线路"
                        class="wid220"
                        @change="getArticleList"
                      >
                        <el-option label="所有" value=""></el-option>
                        <el-option
                          v-for="(line, index) in lines"
                          :key="index"
                          :label="line"
                          :value="index"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                    <div class="clearfix"></div>
                    <el-form-item label=" ">
                      <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="!moredata">
                      </MainFormBtn>
                    </el-form-item>
                  </div>
                </div>
              </el-collapse-transition>
            </el-form>
          </div>
          <div class="main-controls-wrap">
            <div>
              <el-button type="primary" @click="exportform(4)">导出二维表</el-button>
              <el-button type="primary" @click="exportform(1)">导出列表</el-button>
            </div>
          </div>
        </div>
        <!-- 搜索二 -->
        <div class="mainor_form batch-wrap" v-show="activeName == 2">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom2"
            label-width="100px"
          >
            <el-form-item label="" prop="date_type" class="wid100 methods">
              <el-select v-model="form.date_type" placeholder="请选择">
                <el-option label="配送日期" value="1"> </el-option>
                <el-option label="下单日期" value="2"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="" prop="establishTime">
              <el-date-picker
                v-model="establishTime"
                type="daterange"
                class="time220 marginLeft5s"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
                @change="dateChange"
              />
            </el-form-item>
            <el-form-item label="用户：" prop="uid">
              <change-user-input
                class="time220"
                ref="changeUserInput"
                @sendValue="
                  (data) => {
                    form.uid = data.info.user_id;
                  }
                "
                @clear="form.uid = ''"
              ></change-user-input>
            </el-form-item>
            <el-form-item label="支付方式：" prop="is_piroment">
              <el-select
                v-model.trim="form.is_piroment"
                placeholder="支付方式"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(is_piroment, index) in is_piroment"
                  :key="index"
                  :label="is_piroment"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="moredata">
              <el-button @click="exportform(2)">导出</el-button>
            </MainFormBtn>
            <div class="more el-form-item" @click="moredata = !moredata">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-show="moredata">
                <el-form-item label="配送线路：" prop="line_id">
                  <el-select
                    v-model.trim="form.line_id"
                    placeholder="配送线路"
                    class="wid220"
                    @change="getArticleList"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(line, index) in lines"
                      :key="index"
                      :label="line"
                      :value="index"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label=" ">
                  <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="!moredata">
                    <el-button @click="exportform(2)">导出</el-button>
                  </MainFormBtn>
                </el-form-item>
              </div>
            </el-collapse-transition>
            
          </el-form>
        </div>
        <div class="main_table">
          <!-- 表格一 -->
          <div v-if="activeName == 1">
            <el-table
              ref="multipleTable"
              v-loading="listLoading"
              :data="list"
              element-loading-text="加载中"
              fit
              border
              highlight-current-row
              :header-cell-class-name="this.$verticaline"
              :height="borderHeight"
              :header-cell-style="tableHeightStyle"
            >
              <el-table-column
                label="商品名称"
                prop="goods_name"
              width="120"
                show-overflow-tooltip
              >
                <template slot-scope="scope">{{ scope.row.goods_name }}</template>
              </el-table-column>

              <el-table-column
                label="SKU编码"
                prop="goods_spu_code"
                show-overflow-tooltip
              />

              <el-table-column label="单位" prop="unit" show-overflow-tooltip />
              <el-table-column
                label="订单数量"
                prop="order_num"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column
                label="配送数量"
                prop="send_num"
                show-overflow-tooltip
              >
              </el-table-column>
              <el-table-column label="配送金额" prop="send_money" min-width="120">
                <template slot="header">
                  <headerSortArrow
                    label="send_money"
                    ref="arrow1"
                    @handleSort="handleSort($event, 'arrow1')"
                    >配送金额</headerSortArrow
                  >
                </template>
                <template slot-scope="scope">
                  {{ scope.row.send_money | twoPlace }}
                </template>
              </el-table-column>
              <el-table-column label="退单数量" prop="return_order_num">
              </el-table-column>
              <el-table-column label="退货数量" prop="return_num">
              </el-table-column>
              <el-table-column
                label="退货金额"
                prop="return_money"
                min-width="120"
              >
                <template slot="header">
                  <headerSortArrow
                    label="return_money"
                    ref="arrow2"
                    @handleSort="handleSort($event, 'arrow2')"
                    >退货金额</headerSortArrow
                  ></template
                >
                <template slot-scope="scope">
                  {{ scope.row.return_money | twoPlace }}
                </template>
              </el-table-column>
              <el-table-column
                label="实配送金额"
                prop="send_real_money"
              ></el-table-column>
              <el-table-column
                label="管理操作"
                min-width="140"
                prop=""
                fixed="right"
                align="center"
                label-class-name="main-center"
              >
                <template slot-scope="scope">
                  <el-link type="primary" @click="dayReport(scope.row, 1)"
                    >日报表</el-link
                  >
                  <el-link type="primary" @click="dayReport(scope.row, 2)"
                    >用户</el-link
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
          <!-- 表格二 -->
          <div v-if="activeName == 2" v-cloak>
            <el-table
              ref="multipleTable"
              class="mg-t-4"
              v-loading="listLoading"
              :data="list"
              element-loading-text="加载中"
              fit
              border
              highlight-current-row
              :header-cell-class-name="this.$verticaline"
              :height="borderHeight"
              :header-cell-style="tableHeightStyle"
            >
              <el-table-column label="一级分类" prop="category_name">
                <template slot-scope="scope">{{
                  scope.row.category_name
                }}</template>
              </el-table-column>

              <el-table-column label="配送金额" prop="send_money" />
              <el-table-column label="退货金额" prop="return_money" />
              <el-table-column
                label="实配送金额"
                prop="send_real_money"
              ></el-table-column>
              <el-table-column
                label="管理操作"
                min-width="140"
                prop=""
                fixed="right"
                align="center"
                label-class-name="main-center"
              >
                <template slot-scope="scope">
                  <el-link type="primary" @click="dayReportCategory(scope.row, 1)"
                    >日报表</el-link
                  >
                  <el-link type="primary" @click="dayReportCategory(scope.row, 2)"
                    >用户</el-link
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="
                (page) => {
                  changeLimit(page, 1);
                }
              "
              @current-change="
                (page) => {
                  changePage(page, 1);
                }
              "
            />
          </div>
        </div>
      </div>
    </div>

    <!-- 日报表 和  用户 -->
    <el-drawer
      class="report_drawer drawer_min900"
      :title="drawer_header_title"
      size="50%"
      :visible.sync="goods_drawer"
      direction="rtl"
      @closed="drawerClose"
    >
      <div class="drawer_container">
        <div class="drawer_container_header">
          <div class="heder_left">
            <span>{{ drawer_title }}</span>
          </div>
          <div class="header_right">
            <el-button type="primary" @click="exportform(3)">导出</el-button>
          </div>
        </div>
        <div class="drawer_container_main">
          <div class="main_table marginTop10" style="min-height: 400px">
            <el-table
              ref="multipleTable"
              v-loading="drawer_loading"
              :data="drawer_list"
              element-loading-text="加载中"
              fit
              border
              highlight-current-row
              :header-cell-class-name="this.$verticaline"
            >
              <el-table-column
                :label="drawer_type == 1 ? '配送日期' : '用户名称'"
                :prop="drawer_type == 1 ? 'day' : 'nickname'"
              />

              <el-table-column
                v-if="drawer_list[0] && Object.keys(drawer_list[0]).length > 4"
                label="配送数量"
                prop="send_num"
              >
                <template slot-scope="scope">
                  {{ scope.row.send_num }}
                </template>
              </el-table-column>
              <el-table-column label="配送金额" prop="send_money">
                <template slot-scope="scope">
                  {{ scope.row.send_money | twoPlace }}
                </template>
              </el-table-column>
              <!-- 使用v-if控制el-table-column渲染时会出来数据错乱，需要加key或者使用模板 -->
              <el-table-column
                key="1"
                v-if="drawer_list[0] && Object.keys(drawer_list[0]).length > 4"
                label="退货数量"
                prop="return_num"
              />
              <el-table-column label="退货金额" prop="return_money">
                <template slot-scope="scope">
                  {{ scope.row.return_money | twoPlace }}
                </template>
              </el-table-column>
              <el-table-column
                label="实配送金额"
                prop="send_real_money"
              ></el-table-column>
            </el-table>
          </div>
          <div class="main_page boxdq">
            <div></div>
            <!-- 分页 -->
            <div class="main_page_r">
              <MainPagination
                :current-page="drawer_current.page"
                :page-sizes="[20, 30, 50, 100]"
                :page-size="drawer_limit"
                layout="total, sizes, prev, pager, next, jumper"
                :total="drawer_totalCount"
                background
                @size-change="
                  (page) => {
                    changeLimit(page, 2);
                  }
                "
                @current-change="
                  (page) => {
                    changePage(page, 2);
                  }
                "
              />
            </div>
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  get_saleGoods,
  get_saleCategory,
  get_saleGoodsDetail,
  get_saleGoodsMemberDetail,
  export_exportSaleGoods,
  export_exportSaleGoodsDetail,
  export_exportSaleGoodsMemberDet,
  export_exportSaleCategory,
  export_exportGoodsMember,
  get_report_base,
  goods_getAllgroup,
  get_saleCategoryDetail,
  get_saleCategoryMemberDetail,
  export_SaleCategoryDetail,
  export_SaleCategoryMemberDetail,
} from "@/api/report/index";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { report_shortcuts } from "@/options/new_dateOption";
import { parseTime, exportMethod, getNowMonth } from "@/utils/index";
import { recursion, category } from "@/utils/index";
import headerSortArrow from "@/views/report/components/headerSortArrow";
import { user_getMemberInfo } from "@/api/currency";

export default {
  mixins: [mixins],
  components: { headerSortArrow },
  data() {
    return {
      pickerOptions: report_shortcuts,
      exportMethod,
      // 时间
      parseTime,
      getNowMonth,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: [],
      listLoading: false,
      // 分类搜索
      form: {
        start_time: "",
        end_time: "",
        uid: "",
        is_piroment: "",
        date_type: "1",
      },
      // 商品搜索
      goods_form: {
        start_time: "",
        end_time: "",
        category_id: "",
        goods_spu_id: "",
        uid: "",
        is_piroment: "",
        date_type: "1",
        order_by: "",
        order_type: "", //1升序  2降序
        zid: "",
        group_id: "",
        sub_zid: null,
      },
      group_name: "",
      is_piroment: {},
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
      establishTime: [],
      activeName: "1",
      moredata: false,
      moredata2: false,
      goods_drawer: false, //商品明细抽屉
      drawer_current: {
        page: 1,
        per_page: 20,
      },
      drawer_limit: 20,
      drawer_totalCount: 1,
      drawer_list: [{}],
      drawer_loading: false,
      drawer_title: "商品名称",
      drawer_header_title: "每日报表",
      drawer_type: 1,
      drawer_form: {
        start_time: "",
        end_time: "",
        goods_spu_id: "",
        group_id: "",
      },
      address: {
        subAccount: [],
      },
      lines: []
    };
  },
  watch: {
    establishTime: {
      handler(val) {
        this.goods_form.start_time = val[0] ? val[0] : "";
        this.goods_form.end_time = val[1] ? val[1] : "";
        this.form.start_time = val[0] ? val[0] : "";
        this.form.end_time = val[1] ? val[1] : "";
      },
    },
  },
  created() {
    this.getNumerical(330);
    // 列表
    this.establishTime = this.getNowMonth();
    this.get_report_base();
    this.get_sales_list();
  },
  mounted(){
    this.$nextTick(() => {
      if (this.$refs.multipleTable && this.$refs.multipleTable.doLayout) {
        this.$refs.multipleTable.doLayout();
      }
    })
  },
  methods: {
    startGetting() {
      this.getSearchFor();
    },
    // 搜索并折叠搜索条件
    getSearchFor() {
      this.getArticleList();
      // this.moredata = false;
      // this.moredata2 = false;
    },
    // 基本信息
    get_report_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      get_report_base(data).then((res) => {
        if (res.errcode == 0) {
          this.is_piroment = res.data.is_piroment;
        }
      });
    },
    // 获取用户
    getUser(data) {
      let uid = data.info.user_id;
      this.goods_form.uid = uid;
      this.user_getMemberInfo(uid);
    },
    // 清除用户的uid
    clearUid() {
      this.goods_form.uid = "";
      this.address.subAccount = [];
      this.goods_form.sub_zid = "";
      this.goods_form.zid = "";
    },
    get_sales_list() {
      let func = "";
      switch (this.activeName) {
        case "1":
          func = get_saleGoods;
          break;
        case "2":
          func = get_saleCategory;
          break;
      }
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      func(data).then((res) => {
        if (res.errcode == 0) {
          this.list = res.data.list;
          if (this.activeName == 1) {
            this.lines = res.data.line;
          }
          this.totalCount = res.data.total;
          if (res.data.category_list) {
            this.options = recursion(res.data.category_list, true);
          }
        } else {
          this.$message.error(res.errmsg);
        }
        this.listLoading = false;
      });
    },
    //获取子账号
    user_getMemberInfo(uid) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: uid }
      );
      user_getMemberInfo(data).then((res) => {
        if (res.errcode == 0) {
          res.data.subAccount.map((e, ind) => {
            if (ind == 0) {
              e.id = 0;
              e.nickname = "主账号";
            }
          });
          res.data.subAccount.unshift({
            id: "",
            nickname: "所有",
          });
          this.address = res.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },

    // 重置
    reset() {
      let ref = `userListFrom${this.activeName}`;
      this.$refs[ref].resetFields();
      this.establishTime = this.getNowMonth();
      if (this.moredata && this.activeName == "1") {
        this.$refs["changeGoodsInput"].name = "";
        this.$refs["changeUserInput"].name = "";
        this.goods_form.zid = "";
        this.goods_form.group_id = "";
        this.group_name = "";
      }
      if (this.$refs["changeUserInput"]) {
        this.$refs["changeUserInput"].name = "";
      }
      this.get_sales_list();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.get_sales_list();
    },
    // 分页
    changeLimit(pageSize, type) {
      // console.log(pageSize, type,this.activeName);
      switch (type) {
        case 1:
          this.current.per_page = pageSize;
          this.get_sales_list();
          break;
        case 2:
          this.drawer_current.per_page = pageSize;
          if (this.drawer_type == 1) {
            // 根据当前页面判断选择请求数据
            if (this.activeName == 1) {
              this.get_saleGoodsDetail();
            } else {
              this.get_saleCategoryDetail(); //商品分类日报表
            }
          } else if (this.drawer_type == 2) {
            if (this.activeName == 1) {
              this.get_saleGoodsMemberDetail();
            } else {
              this.get_saleCategoryMemberDetail();
            }
          }
          break;
      }
    },
    changePage(page, type) {
      switch (type) {
        case 1:
          this.current.page = page;
          this.get_sales_list();
          break;
        case 2:
          this.drawer_current.page = page;
          if (this.drawer_type == 1) {
            if (this.activeName == 1) {
              this.get_saleGoodsDetail();
            } else {
              this.get_saleCategoryDetail(); //商品分类日报表
            }
          } else if (this.drawer_type == 2) {
            if (this.activeName == 1) {
              this.get_saleGoodsMemberDetail();
            } else {
              this.get_saleCategoryMemberDetail();
            }
          }

          break;
      }
    },

    list_form() {
      let form = null;
      switch (this.activeName) {
        case "1":
          let { sub_zid, ...list } = JSON.parse(
            JSON.stringify(this.goods_form)
          );
          form = list;
          // form.zid = form.zid ? form.zid : 0;
          form.pid = form.category_id[0] ? form.category_id[0] : "";
          form.category_id = category(form.category_id);
          break;
        case "2":
          form = JSON.parse(JSON.stringify(this.form));
          break;
      }

      form.start_time = this.establishTime[0] ? this.establishTime[0] : "";
      form.end_time = this.establishTime[1] ? this.establishTime[1] : "";

      return form;
    },
    // 排序
    handleSort(data, ref) {
      let arr = ["arrow1", "arrow2"].filter((e) => e != ref);
      arr.map((e) => {
        this.$refs[e].selected = false;
      });
      this.goods_form.order_by = data.label;
      this.goods_form.order_type = data.type;
      this.get_sales_list();
    },

    handleClick(tab, event) {
      this.list = [];
      if (this.activeName == 1) {
        this.current.page = 1;
        this.get_sales_list();
      } else if (this.activeName == 2) {
        this.current.page = 1;
        this.get_sales_list();
      }
    },

    querySearchAsync(queryString, cb) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { title: queryString.trim() })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { title: queryString.trim() }
      );
      goods_getAllgroup(data).then((res) => {
        if (res.errcode == 0) {
          var results = res.data.list;
          cb(results);
        } else {
          this.$message.error(res.errmsg);
          cb([]);
        }
      });
    },
    basicChange(item) {
      this.group_name = item.title;
      this.goods_form.group_id = item.id;
    },
    // 导出
    exportform(type) {
      switch (type) {
        case 1:
          this.export_exportSaleGoods();
          break;
        case 2:
          this.export_exportSaleCategory();
          break;
        case 3:
          if (this.drawer_type == 1) {
            this.export_exportSaleGoodsDetail();
          } else if (this.drawer_type == 2) {
            this.export_exportSaleGoodsMemberDet();
          }
          break;
        case 4:
          this.export_exportGoodsMember();
          break;
      }
    },
    // 商品导出
    export_exportSaleGoods() {
      let start = new Date(this.list_form().start_time).getTime();
      let end = new Date(this.list_form().end_time).getTime();
      let diff = start - end;
      if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
        this.$message.warning("选择时间跨度不能超过两个月");
        this.establishTime = this.getNowMonth();
      } else {
        let data = Object.assign(
          {
            sign: Md5(
              objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
            ),
          },
          apiUrl().name,
          this.list_form()
        );
        const notify = this.$notify.info({
          title: "开始导出...",
          type: "success",
          duration: 0,
        });
        export_exportSaleGoods(data).then((res) => {
          this.exportMethod(res, "销售报表-商品", notify);
        });
      }
    },
    //二维表导出
    export_exportGoodsMember() {
      let start = new Date(this.list_form().start_time).getTime();
      let end = new Date(this.list_form().end_time).getTime();
      let diff = start - end;
      if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
        this.$message.warning("选择时间跨度不能超过两个月");
        this.establishTime = this.getNowMonth();
      } else {
        let data = Object.assign(
          {
            sign: Md5(
              objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
            ),
          },
          apiUrl().name,
          this.list_form()
        );
        const notify = this.$notify.info({
          title: "开始导出...",
          type: "success",
          duration: 0,
        });
        export_exportGoodsMember(data).then((res) => {
          this.exportMethod(res, "二维表-商品", notify);
        });
      }
    },

    // 商品日报表导出
    export_exportSaleGoodsDetail() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.drawer_form
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      if (this.activeName == 1) {
        export_exportSaleGoodsDetail(data).then((res) => {
          this.exportMethod(res, "销售报表-商品-日报表", notify);
        });
      } else {
        export_SaleCategoryDetail(data).then((res) => {
          this.exportMethod(res, "销售报表-分类-日报表", notify);
        });
      }
    },
    // 商品用户导出
    export_exportSaleGoodsMemberDet() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.drawer_form
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      if (this.activeName == 1) {
        export_exportSaleGoodsMemberDet(data).then((res) => {
          this.exportMethod(res, "销售报表-商品-用户", notify);
        });
      } else {
        export_SaleCategoryMemberDetail(data).then((res) => {
          this.exportMethod(res, "销售报表-商品分类-用户", notify);
        });
      }
    },
    // 分类导出
    export_exportSaleCategory() {
      let start = new Date(this.list_form().start_time).getTime();
      let end = new Date(this.list_form().end_time).getTime();
      let diff = start - end;
      if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
        this.$message.warning("选择时间跨度不能超过两个月");
        this.establishTime = this.getNowMonth();
      } else {
        let data = Object.assign(
          {
            sign: Md5(
              objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
            ),
          },
          apiUrl().name,
          this.form
        );
        const notify = this.$notify.info({
          title: "开始导出...",
          type: "success",
          duration: 0,
        });
        export_exportSaleCategory(data).then((res) => {
          this.exportMethod(res, "销售报表-分类", notify);
        });
      }
    },

    // 日期选择
    dateChange(val) {
      let start = new Date(val[0]).getTime();
      let end = new Date(val[1]).getTime();
      let diff = start - end;
      if (Math.abs(diff) > 3600 * 1000 * 24 * 365) {
        this.$message.warning("选择时间跨度不能超过一年");
        this.establishTime = this.getNowMonth();
      }
      this.get_sales_list();
    },
    // 商品列表 日报表 和 用户报表
    dayReport(row, type) {
      // console.log(row);
      this.drawer_type = type;
      switch (type) {
        case 1:
          this.drawer_header_title = "每日报表";
          break;
        case 2:
          this.drawer_header_title = "用户报表";
          break;
      }
      this.drawer_title = row.goods_name;
      this.drawer_form = {
        start_time: "",
        end_time: "",
        uid: this.goods_form.uid,
        zid: this.goods_form.zid,
        goods_spu_id: row.goods_spu_id,
        is_piroment: this.goods_form.is_piroment,
        date_type: this.goods_form.date_type,
        group_id: this.goods_form.group_id,
      };
      if (this.establishTime) {
        this.drawer_form.start_time = this.establishTime[0]
          ? this.establishTime[0]
          : "";
        this.drawer_form.end_time = this.establishTime[1]
          ? this.establishTime[1]
          : "";
      }
      switch (type) {
        case 1:
          this.get_saleGoodsDetail();
          break;
        case 2:
          this.get_saleGoodsMemberDetail();
          break;
      }
      this.goods_drawer = true;
    },
    // 商品分类 日报表 和 用户报表
    dayReportCategory(row, type) {
      this.drawer_type = type;
      switch (type) {
        case 1:
          this.drawer_header_title = "每日报表";
          break;
        case 2:
          this.drawer_header_title = "用户报表";
          break;
      }
      this.drawer_title = row.category_name;
      this.drawer_form = {
        start_time: "",
        end_time: "",
        uid: this.form.uid,
        // zid: this.goods_form.zid,
        is_piroment: this.form.is_piroment,
        date_type: this.form.date_type,
        pid: row.pid,
      };
      if (this.establishTime) {
        this.drawer_form.start_time = this.establishTime[0]
          ? this.establishTime[0]
          : "";
        this.drawer_form.end_time = this.establishTime[1]
          ? this.establishTime[1]
          : "";
      }
      switch (type) {
        case 1:
          this.get_saleCategoryDetail();
          break;
        case 2:
          this.get_saleCategoryMemberDetail();
          break;
      }
      this.goods_drawer = true;
    },
    // 商品分类 每日明细
    get_saleCategoryDetail() {
      this.drawer_loading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.drawer_form,
        this.drawer_current
      );
      get_saleCategoryDetail(data).then((res) => {
        if (res.errcode == 0) {
          this.drawer_list = res.data.list;
          this.drawer_totalCount = res.data.total;
          this.drawer_loading = false;
        } else {
          this.$message.warning(res.errmsg);
        }
      });
    },
    // 商品分类 用户明细
    get_saleCategoryMemberDetail() {
      this.drawer_loading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.drawer_form,
        this.drawer_current
      );
      get_saleCategoryMemberDetail(data).then((res) => {
        if (res.errcode == 0) {
          this.drawer_list = res.data.list;
          this.drawer_totalCount = res.data.total;
          this.drawer_loading = false;
        } else {
          this.$message.warning(res.errmsg);
        }
      });
    },
    // 商品列表 每日明细
    get_saleGoodsDetail() {
      this.drawer_loading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.drawer_form,
        this.drawer_current
      );
      get_saleGoodsDetail(data).then((res) => {
        if (res.errcode == 0) {
          this.drawer_list = res.data.list;
          this.drawer_totalCount = res.data.total;
          this.drawer_loading = false;
        } else {
          this.$message.warning(res.errmsg);
        }
      });
    },
    // 商品列表 用户明细
    get_saleGoodsMemberDetail() {
      this.drawer_loading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.drawer_form,
        this.drawer_current
      );
      get_saleGoodsMemberDetail(data).then((res) => {
        if (res.errcode == 0) {
          this.drawer_list = res.data.list;
          this.drawer_totalCount = res.data.total;
          this.drawer_loading = false;
        } else {
          this.$message.warning(res.errmsg);
        }
      });
    },
    drawerClose() {
      this.drawer_list = [{}];
      this.drawer_current.page = 1;
      this.drawer_current.per_page = 20;
      this.drawer_limit = 20;
      this.drawer_form = {
        start_time: "",
        end_time: "",
        goods_spu_id: "",
        is_piroment: "",
        group_id: "",
      };
    },
    //选择子账号事件
    // sub_account(val) {
    //   this.goods_form.zid = this.address.subAccount[val].id;
    // },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: 100%;
  }
  .mainor_form {
    &.batch-wrap {
      form {
        width: calc(100% - 280px);
      }
    }
  }
}

// 支付状态
.main_ti {
  display: flex;
  align-items: center;
  flex-direction: row;
  margin-bottom: 15px;
  padding-bottom: 1px;
  position: relative;
  .back {
    padding: 0 55px 0 0;
    font-size: 14px;
    color: #03a98e;
    display: flex;
    align-items: center;
    flex-direction: row;
    font-size: 0;
    a {
      font-size: 14px;
      color: #03a98e;
    }
    span {
      font-size: 14px;
      padding: 0 14px;
      transform: translateY(-1px);
    }
  }
  /deep/.el-tabs__header {
    margin: 0;
  }
  /deep/.el-checkbox {
    margin-top: 12px;
  }
  &::after {
    height: 1px;
    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: #e4e7ed;
    z-index: 1;
  }
  /deep/.el-tabs__nav-wrap::after {
    height: 0;
  }
}

.wid100 {
  width: 100px;
}

.marginLeft5s {
  margin-left: -5px;
}
/deep/.methods .el-input__inner {
  border: none;
}
/deep/ .el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
